import { create } from 'zustand';

export interface Plugin {
  id: string;
  name: string;
  version: string;
  description: string;
  author: string;
  enabled: boolean;
  type: string;
  tags: string[];
  icon?: string;
  configurable: boolean;
  installDate: string;
  updateDate?: string;
}

interface PluginState {
  plugins: Plugin[];
  loading: boolean;
  error: string | null;
  fetchPlugins: () => Promise<void>;
  togglePlugin: (id: string) => void;
  installPlugin: (plugin: Omit<Plugin, 'id' | 'installDate'>) => Promise<void>;
  uninstallPlugin: (id: string) => Promise<void>;
  updatePluginSettings: (id: string, settings: any) => Promise<void>;
}

// 模拟插件数据
const mockPlugins: Plugin[] = [
  {
    id: 'pdf-extractor',
    name: 'PDF文本提取器',
    version: '1.2.0',
    description: '高级PDF文本和表格提取工具，支持多种格式和布局。',
    author: '知识工具团队',
    enabled: true,
    type: '文档处理',
    tags: ['PDF', '提取', '文本'],
    configurable: true,
    installDate: '2023-05-10',
    updateDate: '2023-09-20',
  },
  {
    id: 'semantic-search',
    name: '语义搜索增强',
    version: '0.9.5',
    description: '使用高级语义模型增强系统搜索能力，提供更精准的结果。',
    author: 'AI搜索实验室',
    enabled: true,
    type: '搜索增强',
    tags: ['搜索', '语义', 'AI'],
    configurable: true,
    installDate: '2023-07-15',
  },
  {
    id: 'knowledge-graph',
    name: '知识图谱生成器',
    version: '1.0.0',
    description: '从文档中自动生成知识图谱，显示概念间关系。',
    author: '图谱技术公司',
    enabled: false,
    type: '知识管理',
    tags: ['图谱', '关系', '可视化'],
    configurable: true,
    installDate: '2023-08-01',
  },
];

export const usePluginStore = create<PluginState>((set, get) => ({
  plugins: mockPlugins,
  loading: false,
  error: null,
  
  fetchPlugins: async () => {
    set({ loading: true, error: null });
    
    try {
      // 模拟API请求
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set({ plugins: mockPlugins, loading: false });
    } catch (error) {
      set({ error: '获取插件失败', loading: false });
    }
  },
  
  togglePlugin: (id: string) => {
    set(state => ({
      plugins: state.plugins.map(plugin => 
        plugin.id === id ? { ...plugin, enabled: !plugin.enabled } : plugin
      )
    }));
  },
  
  installPlugin: async (plugin) => {
    set({ loading: true, error: null });
    
    try {
      // 模拟API请求
      await new Promise(resolve => setTimeout(resolve, 500));
      
      const newPlugin: Plugin = {
        ...plugin,
        id: `plugin-${Date.now()}`,
        installDate: new Date().toISOString().split('T')[0],
      };
      
      set(state => ({
        plugins: [...state.plugins, newPlugin],
        loading: false
      }));
    } catch (error) {
      set({ error: '安装插件失败', loading: false });
    }
  },
  
  uninstallPlugin: async (id: string) => {
    set({ loading: true, error: null });
    
    try {
      // 模拟API请求
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set(state => ({
        plugins: state.plugins.filter(plugin => plugin.id !== id),
        loading: false
      }));
    } catch (error) {
      set({ error: '卸载插件失败', loading: false });
    }
  },
  
  updatePluginSettings: async (id: string, settings: any) => {
    set({ loading: true, error: null });
    
    try {
      // 模拟API请求
      await new Promise(resolve => setTimeout(resolve, 500));
      
      // 实际操作中，这里应该根据具体设置更新插件的相关属性
      set({ loading: false });
    } catch (error) {
      set({ error: '更新插件设置失败', loading: false });
    }
  },
})); 